<?php

namespace App\Http\Controllers\Auth;

use App\Http\Common\Globals;
use App\Http\Common\MyUtils;
use App\Http\Controllers\BaseController;
use App\Http\Requests\Admin\LoginRequest;
use App\Models\Front\User;
use Illuminate\Support\Facades\Session;

class FrontLoginController extends BaseController
{
    public function __construct()
    {
        parent::__construct();
        $this->model = new User();
    }

    /**
     * 前端小程序用户 登录调用的接口
     * @param LoginRequest $request
     * @return string
     */
    public function login(LoginRequest $request){
        Globals::$error = '';
        // 获取openID unionId之类
        // 登录，利用用户名称以及密码来进行登录
        $this->model->login($request->all());
        return $this->dealData();
    }

    /**
     * 更新用户信息
     */
    public function updateUserInfo(){
        $this->model->updateUserInfo($this->request->request->all());
        return $this->dealData();
    }
    /**
     * 处理数据，将数据返给前端
     */
    public function dealData(){
        $data['token'] = Session::get("_token");
        $data['roles'] = Session::get("roles");
        $data['staffs'] = Session::get("staffs");
        $data['groups'] = Session::get("groups");
        $data['username'] = Session::get("username");
        $data['nickname'] = Session::get("nickname");
        $data['avatar'] = Session::get("avatar");
        $data['logo'] = Session::get("logo");
        $data['userType'] = Session::get("userType");
        $data['rooms'] = Session::get("rooms");
        if (!Globals::$error){// 登录成功
            MyUtils::writeLogs("登录操作","小程序普通操作","2",'小程序登录成功');// 写入日志，登录成功
            return MyUtils::dealFD($data,1);
        }else{
            MyUtils::writeLogs("登录操作","小程序普通操作","2",Globals::$error);// 写入日志，登录成功
            return MyUtils::dealFD(Globals::$error,-1);
        }
    }

}
